<?php
/*
* 自动分析url参数分页类[替换方法], 在 windows sp2 + apache2.2 + php5.2.10 + mysql5.1.36测试通过
* author:pirate
* createDate:2009-08-08
* modifyDate:2009-08-14  增加了第2种模式:数字分页, 参考paipai分页模式, 可有23种样式供选择, 默认为yahoo2, 且支持Zend Framework 分页
* 注：请勿用于商业用途  本人只为测试, 速度暂放
* 示例请查看class.pager.exampler.php
* 如有任何问题请入群提问：51062865
*/

class Edm_Pager {
    //当前页
    var $pageNo = 1;
    //页总数
    var	$pageCount = 1;
    //页大小
    var	$pageSize = 10;
    //记录数
    var $recordCount = 1;
    //分页变量
    var $pageStr = 'page';
    //分页连接字符串
    var $firstPage = '';
    var $nextPage = '';
    var $prevPage = '';
    var $lastPage = '';
    //分页显示, 初始化
    var $firstHref = '<span>首页</span>';
    var $nextHref = '<span>下一页</span>';
    var $prevHref = '<span>下一页</span>';
    var $lastHref = '<span>尾页</span>';
    //状态
    var $isBool = false;
    //当前文件名
    var $thisFile;
    //当前查询参数
    var $thisParams;
    //错误信息
    var $err = '';

    //初始化
    public function Pager($myRecordCount = 1, $myPageSize = 10, $myPageStr = 'page') {
        if (!is_numeric($myRecordCount) || (!is_numeric($myPageSize))) {
            $this->err = 'params error.';
            $this->halt();
        }
        $this->recordCount = $myRecordCount;
        $this->pageSize = $myPageSize;
        $this->pageCount = @ceil($myRecordCount / $myPageSize);
        $this->pageStr = $myPageStr;
        $this->setPageUrl();
    }

    //处理url
    public function setPageUrl() {
        $pageStr = $this->pageStr;
        $myPageNo = $this->getPageNo();
        $nextPageNo = $myPageNo + 1;
        $prevPageNo = $myPageNo - 1;
        $replace = $this->pageStr . '=' . $myPageNo;

        $myFile = $_SERVER['REQUEST_URI'];
        $myUrl = $_SERVER['QUERY_STRING'];
        if (empty($myUrl)) {
            $myFile = '?' . $replace;
        } elseif (!preg_match("/$replace/", $myUrl)) {
            $myFile = '?' . $myUrl . '&' . $replace;
        }

        $this->thisFile = $myFile;
        $this->thisParams = $myFile;
        $this->firstPage =  str_replace($replace, $pageStr . '=1', $myFile);
        $this->nextPage = str_replace($replace, $pageStr . '=' . $nextPageNo, $myFile);
        $this->prevPage =  str_replace($replace, $pageStr . '=' . $prevPageNo, $myFile);
        $this->lastPage =  str_replace($replace, $pageStr . '=' . $this->pageCount, $myFile);
    }

    //获取当前页码
    public function getPageNo() {
        $myPageNo = @intval($_GET[$this->pageStr]);
        if ($myPageNo >= 1 && $myPageNo <= $this->pageCount) {
            $this->pageNo = $myPageNo;
            $this->isBool = true;
        }
        return $this->pageNo;
    }

    //连接处理
    public function getLink($linkUrl, $linkName, $linkCss = '') {
        return '<span><a class="' . $linkCss . '" href="' . $linkUrl . '">' . $linkName . '</a></span>';
    }

    //下拉列表跳转页 ，适合于后台
    public function getSelectPage($divCss = '') {
        $myPageCount = @$this->pageCount;
        $myFile = $this->thisFile;
        $myParams = $this->thisParams;
        $myPageStr = $this->pageStr;
        $myPageNo = $this->pageNo;
        $myReplace = $myPageStr . '=' . $myPageNo;
        $str = '';

        if ($myPageNo > 1) {
            $this->firstHref = $this->getLink($this->firstPage, '首页');
            $this->prevHref = $this->getLink($this->prevPage, '上一页');
        }
        if ($myPageNo < $myPageCount) {
            $this->nextHref = $this->getLink($this->nextPage, '下一页');
            $this->lastHref = $this->getLink($this->lastPage, '尾页');
        }
        $str = $this->firstHref . $this->prevHref . $this->nextHref . $this->lastHref;
        $str .= '<span>共' . $this->recordCount . '条记录, ' . $myPageCount . '页</span>';
        if ($myPageCount > 0) {
            $str .= '<span>转到 ';
            $str .= '<select name = "__selectPage' . $myPageCount . '" onChange = "javascript:location.href = this.value">';
            for ($i = 1; $i <= $myPageCount; $i++) {
                $str .= '<option';
                $str .= $myPageNo == $i ? ' value = "' . $i . '" selected':' value = "' . str_replace($myReplace, $myPageStr . '=' . $i, $myParams) . '"';
                $str .= '>';
                $str .= $i . '</option>';
            }
            $str .= '</select></span>';
        }
        return '<div class="' . $divCss . '">' . $str . '</div>';
    }

    //数字式分页
    public function getNumberPage ($divCss = '') {
        $myPageCount = @$this->pageCount;
        $myFile = $this->thisFile;
        $myParams = $this->thisParams;
        $myPageStr = $this->pageStr;
        $myPageNo = $this->pageNo;
        //$myPageCount = 10;
        $myReplace = $myPageStr . '=' . $myPageNo;
        $myPrevPage = $myPageNo > 1 ? $myPageNo - 1 : 1;
        $myNextPage = $myPageNo < $myPageCount && $myPageNo != $myPageCount ? $myPageNo + 1 : $myPageCount;
        $str = '<div class="' . $divCss . '">';
        $str .=  $myPageNo == 1 ? '<span class="disabled">上一页</span>' : '';
        $str .=  $myPageNo > 1 ? '<a href="' . $this->prevPage . '">上一页</a>':'';
        if ($myPageCount > 10 && $myPageNo >= 9 && ($myPageNo + 4) < $myPageCount) {

            $str .= '<a href="' . str_replace($myReplace, $myPageStr . '=1', $myParams) . '">1</a> .  .  . ';
            $endI = $myPageNo + 3;
            $endI = $endI >= $myPageCount ? $myPageCount : $endI;
            $startI = $endI - 6;
            for($i = $startI; $i <= $endI; $i++) {
                $str .= $myPageNo == $i ?
                        '<span class="current">' . $i . '</span>' :
                        '<a href="' .  str_replace($myReplace, $myPageStr . '=' . $i, $myParams) . '">' . $i . '</a>';
            }
            $str .= '. . . <a href="' . str_replace($myReplace, $myPageStr . '=' . $myPageCount, $myParams) . '">' . $myPageCount . '</a>';

        } elseif ($myPageCount > 10 && $myPageNo < 9) {
            for($i = 1; $i <= 9; $i++) {
                $str .= $myPageNo == $i ?
                        '<span class="current">' . $i . '</span>' :
                        '<a href="' . str_replace($myReplace, $myPageStr . '=' . $i, $myParams) . '">' . $i . '</a>';
            }
            $str .= '. . . <a href="' . str_replace($myReplace, $myPageStr . '=' . $myPageCount, $myParams) . '">' . $myPageCount . '</a>';

        } elseif ($myPageCount > 10 && ($myPageNo + 4) >= $myPageCount) {
            $str .= '<a href="' . str_replace($myReplace, $myPageStr . '=1', $myParams) . '">1</a> .  .  . ';
            for($i = $myPageCount - 7;$i <= $myPageCount; $i++) {
                $str .= $myPageNo == $i ?
                        '<span class="current">' . $i . '</span>' :
                        '<a href="' . str_replace($myReplace, $myPageStr . '=' . $i, $myParams) . '">' . $i . '</a>';
            }
        } elseif ($myPageCount <= 10) {
            for($i = 1; $i <= $myPageCount; $i++) {
                $str .= $myPageNo == $i ?
                        '<span class="current">' . $i . '</span>' :
                        '<a href="' . str_replace($myReplace, $myPageStr . '=' . $i, $myParams) . '">' . $i . '</a>';
            }
        }

        $str .=  $myPageCount == $myPageNo ? '<span class="disabled">下一页</span>' : '';
        $str .=  $myPageCount>$myPageNo ? '<a href="' . $this->nextPage . '">下一页</a>':'';
        $str .= '</div>';
        return $str;
    }

    //显示
    public function display($model = 1, $divCss = '') {
        $str = '';
        //第一种样式
        if ($model == 1) {
            $str = $this->getSelectPage('general');
        }

        //第二种样式
        if ($model == 2) {
            $divCss =  $divCss == '' ? 'yahoo2' : $divCss;
            $str = $this->getNumberPage($divCss);
        }
        return $str;
    }

    //错误处理
    public function halt() {
        die($this->err);
    }
}

